﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Model;
using VAdvantage.Logging;
using System.ComponentModel;
using System.IO;
using System.Windows.Media.Imaging;
using VAdvantage.DataBase;

namespace VAdvantage.Dialogs
{
    public partial class VImageDialog : ChildWindow
    {

        #region Declaration

        /**  Image Model            */
        private MImage _mImage = null;
        /**	Logger					*/
        private static VLogger log = VLogger.GetVLogger(typeof(VImageDialog).FullName);

        private int oldImageId = 0;

        private BackgroundWorker worker = null;

        #endregion



        public VImageDialog()
        {
            InitializeComponent();
            LayoutRoot.Background = new SolidColorBrush(GlobalVariable.WINDOW_BACK_COLOR);
        }


        /// <summary>
        /// Default const
        /// </summary>
        /// <param name="mImage"></param>
        public VImageDialog(MImage mImage):this()
        {
            
            log.Info("MImage=" + mImage);
            _mImage = mImage;
           
            //  load data
            if (_mImage == null)
            {
                this.Cursor = Cursors.Wait;
                worker = new BackgroundWorker();
                worker.DoWork +=(sender,e) =>
                    {
                        _mImage = MImage.Get(Utility.Envs.GetCtx(), 0);
                        //this.Title = Utility.Msg.Translate(Utility.Env.GetCtx(), "AD_Image_ID"));
                    };
                worker.RunWorkerCompleted += (sender, ec) =>
                    {
                        btnFile.Content = _mImage.GetName();
                        img.Source = _mImage.GetImage();
                        oldImageId = _mImage.GetAD_Image_ID();
                        this.Cursor = Cursors.Arrow;
                    };
                worker.RunWorkerAsync();
            }

            else
            {

                btnFile.Content = _mImage.GetName();
                img.Source = _mImage.GetImage();
                oldImageId = _mImage.GetAD_Image_ID();
                //imageLabel.setIcon(m_mImage.getIcon());
            }
            this.Show();
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            
            this.Cursor = Cursors.Wait;

            worker = new BackgroundWorker();

            worker.DoWork += (sd, ed) =>
                {
                    ed.Result = _mImage.Save();
                };
            worker.RunWorkerCompleted += (sc, ec) =>
                {
                    if (ec.Result != null && (bool)ec.Result)
                    {
                        this.DialogResult = true;
                        //this.Close();
                    }
                    else
                    {

                    }
                    this.Cursor = Cursors.Arrow;
                };
            worker.RunWorkerAsync();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            _mImage = null;
            this.DialogResult = false;
            
            //this.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            byte[] data = null;
            String str = "";

            bool? result = fd.ShowDialog();

            if (result != null && (bool)result)
            {
                try
                {
                    System.IO.FileInfo file = fd.File;
                    str =  file.Name;
                    Stream strm = file.OpenRead();//  .OpenFile();
                    BinaryReader br = new BinaryReader(strm);
                    BitmapImage source = new BitmapImage();
                    //img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    data = br.ReadBytes(Convert.ToInt32(strm.Length));// .GetBuffer();
                    source.SetSource(strm);
                    br.Close();
                    img.Source = source;
                    //picBox.Image = img;
                }
                catch (Exception ex)
                {
                    log.Log(Level.WARNING, "load image", ex);
                    return;
                }

                btnFile.Content = str;
                _mImage.SetName(str);
                _mImage.SetImageURL(str);
                _mImage.SetBinaryData(data);

                //this.Height = 126 + picBox.PreferredSize.Height;
            } 
        }

        /// <summary>
        /// Get Image ID
        /// </summary>
        /// <returns>Image id</returns>
        public int GetAD_Image_ID()
        {
            if (_mImage != null)
                return _mImage.GetAD_Image_ID();
            return 0;
        }
    }
}

